package com.heima.controller;

import com.heima.common.Result;
import com.heima.domain.dto.NodeDto;
import com.heima.domain.vo.NodeVo;
import com.heima.service.NodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/vm-service/node")
@Api(tags = "点位管理")
public class NodeController {

    @Autowired
    private NodeService nodeService;

    @GetMapping("/search")
    @ApiOperation("点位搜索")
    public Result getNodeByPage(@RequestParam(required = false) Integer pageIndex,
                                @RequestParam(required = false) Integer pageSize,
                                @RequestParam(required = false) String name,
                                @RequestParam(required = false) Long regionId){
        Result result = nodeService.getNodeByPage(pageIndex,pageSize,name,regionId);
        return result;
    }

    @PostMapping
    @ApiOperation("新增点位")
    public Boolean addNode(@RequestBody NodeDto dto){
        Boolean flag = nodeService.addNode(dto);
        return flag;
    }

    @PutMapping("/{nodeId}")
    @ApiOperation("修改点位")
    public Boolean updateNode(@PathVariable String nodeId,@RequestBody NodeDto dto){
        Boolean flag = nodeService.updateNode(Long.parseLong(nodeId),dto);
        return flag;
    }

    @DeleteMapping("/{nodeId}")
    @ApiOperation("删除点位")
    public Boolean deleteNode(@PathVariable String nodeId){
        Boolean flag = nodeService.deleteNode(Long.parseLong(nodeId));
        return flag;
    }

    @GetMapping("/vmList/{nodeId}")
    @ApiOperation("获取点位详情")
    public List<NodeVo> getByNodeId(@PathVariable String nodeId){
        List<NodeVo> vo = nodeService.getByNodeId(Long.parseLong(nodeId));
        return vo;
    }

    @GetMapping("/count")
    @ApiOperation("点位数量")
    public Long getNodeCount(){
        long count = nodeService.count();
        return count;
    }
}
